package cc.rengu.igas.channel.upqc.core.service.trans;


import cc.rengu.igas.channel.upqc.common.constant.UpqcParamConstant;
import cc.rengu.igas.channel.upqc.common.enums.RespCodeEnum;
import cc.rengu.igas.channel.upqc.common.constant.UpqcTreeNodeConstant;
import cc.rengu.igas.channel.upqc.core.model.ChnlBizResponse;
import cc.rengu.igas.channel.upqc.core.service.base.PayService;
import cc.rengu.igas.channel.upqc.facade.bean.PayerInfo;
import cc.rengu.igas.channel.upqc.facade.request.C2BPreAuthRevsalRequest;
import cc.rengu.igas.channel.upqc.facade.response.C2BPreAuthRevsalResponse;
import cc.rengu.oltp.service.common.constant.AppParamConstant;
import cc.rengu.oltp.service.common.constant.TreeNodeConstant;
import cc.rengu.oltp.service.common.entity.DstChannelInfo;
import cc.rengu.oltp.service.model.BizException;
import cc.rengu.oltp.utility.util.ConvertUtil;
import cc.rengu.oltp.utility.util.RandomUtil;
import cc.rengu.oltp.utility.util.XmlTreeUtil;


/**
 * 被扫预授权冲正
 */
public class C2BPreAuthRevsalService extends PayService {

    @Override
    protected Object beforeBizProcess() throws Exception {
        C2BPreAuthRevsalRequest c2BPreAuthRevsalRequest = new C2BPreAuthRevsalRequest();
        ConvertUtil.convertOutput(c2BPreAuthRevsalRequest);
        return c2BPreAuthRevsalRequest;
    }

    @Override
    protected ChnlBizResponse callBizService(Object request) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        int iReturnCode = 0;
        DstChannelInfo dstChannelInfo = (DstChannelInfo) xmlTreeUtil.getXmlTreeObjectValue(UpqcTreeNodeConstant.DST_CHANNEL_INFO, DstChannelInfo.class);
        C2BPreAuthRevsalResponse c2BPreAuthRevsalResponse = new C2BPreAuthRevsalResponse();
        C2BPreAuthRevsalRequest c2BPreAuthRevsalRequest = (C2BPreAuthRevsalRequest) request;
        /* 根据微信支付产品类型对接口接口字段赋值 */
        ConvertUtil.convertInput(c2BPreAuthRevsalRequest);

        /* 演示环境发送短信默认返回成功 */
        if (AppParamConstant.DEFAULT_ENV.equals(AppParamConstant.APPLICATION_ENV)) {
            ChnlBizResponse chnlBizResponse = new ChnlBizResponse();
            c2BPreAuthRevsalResponse.setRespCode(RespCodeEnum.TRANS_SUCCESS.getRespCode());
            c2BPreAuthRevsalResponse.setRespDesc(RespCodeEnum.TRANS_SUCCESS.getRespDesc());
            chnlBizResponse.setResult(c2BPreAuthRevsalResponse);
            return chnlBizResponse;
        }

        /* 根据通道标识调用不同通道进行支付 */
        xmlTreeUtil.setXmlTreeStringValue(UpqcTreeNodeConstant.txnType, c2BPreAuthRevsalRequest.getHeader().getTxnNum());
        xmlTreeUtil.setXmlTreeStringValue(TreeNodeConstant.TXN_NUM, c2BPreAuthRevsalRequest.getHeader().getTxnNum());
        iReturnCode = schedule.callService(dstChannelInfo.getDstServiceName(), 0);
        /* 恢复原交易码 */
        if (98 == iReturnCode) {
            rglog.error("调用银联二维码服务<{}>超时，iReturnCode:<{}>", dstChannelInfo.getDstServiceName(), iReturnCode);
            c2BPreAuthRevsalResponse.setRespCode("04");
            c2BPreAuthRevsalResponse.setRespDesc("交易状态未明");
        } else if (0 != iReturnCode) {
            rglog.error("调用银联二维码服务<{}>失败，iReturnCode:<{}>", dstChannelInfo.getDstServiceName(), iReturnCode);
            throw new BizException(RespCodeEnum.CALL_BIZ_SERVICE_ERROR.getRespCode(), RespCodeEnum.CALL_BIZ_SERVICE_ERROR.getRespDesc());
        }
        ConvertUtil.convertOutput(c2BPreAuthRevsalResponse);
        ChnlBizResponse chnlBizResponse = new ChnlBizResponse();
        if (UpqcParamConstant.SUCCESS_CODE.equals(c2BPreAuthRevsalResponse.getRespCode())) {
            c2BPreAuthRevsalResponse.setRespCode(RespCodeEnum.TRANS_SUCCESS.getRespCode());
            c2BPreAuthRevsalResponse.setRespDesc(RespCodeEnum.TRANS_SUCCESS.getRespDesc());
        }
        chnlBizResponse.setResult(c2BPreAuthRevsalResponse);
        return chnlBizResponse;
    }

    @Override
    protected void afterBizProcess(ChnlBizResponse response) throws Exception {
        C2BPreAuthRevsalResponse c2BPreAuthRevsalResponse = (C2BPreAuthRevsalResponse) response.getResult();
        ConvertUtil.convertInput(c2BPreAuthRevsalResponse);
    }

}
